package com.wgn.reportquery.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.wgn.reportquery.cons.Const;
import com.wgn.reportquery.dao.QueryDaoImple;
import com.wgn.reportquery.http.QueryHttpClient;
import com.wgn.reportquery.model.Reportdata;
import com.wgn.reportquery.rj.model.Indicatorsresult;
import com.wgn.reportquery.rj.model.RJQueryReport;
import com.wgn.reportquery.rj.model.RJQueryReports;
import com.wgn.reportquery.rj.model.RJReport;
import com.wgn.reportquery.rj.model.RJReportInfo;
import com.wgn.reportquery.rj.model.RJReports;
import com.wgn.reportquery.rj.model.Report;
import com.wgn.reportquery.utils.ReportUtils;

public class SignService {
	public void addSession(String username, String passwd, HttpSession session) {
		session.setAttribute(Const.userName, username);
		session.setAttribute(Const.passwd, passwd);
	}

	public SignService() {
		if (null == dao)
			dao = new QueryDaoImple();
	}

	QueryDaoImple dao;
	public List<String> getRjCards(String username) {
		// TODO Auto-generated method stub
		return dao.getCards(username, "rj");
	}
	static String failback = "登陆失败:用户不存在!名字或密码是不是输错了?是不是没注册？";
	static String failregback = "注册失败,用户名已经存在!";

	public String getSignResult(String userName, String passwd,
			HttpSession session) throws Exception {
		StringBuffer sf = new StringBuffer();
		boolean result = dao.sign(userName, passwd);
		if (result) {
			addSession(userName, passwd, session);
			sf.append("登陆成功!欢迎" + userName);
		} else
		{
			throw new Exception(failback);
			
		}
		return sf.toString();
	}

	static String getReportSqlfront = "SELECT * FROM DATAS a WHERE a.`USERNAME`='";
	static String getReportSqltail = "' ORDER BY a.`REPORTDATE` DESC LIMIT 0,5";

	// <tr>
	// <td>1</td>
	// <td>Mark</td>
	// <td>Otto</td>
	// <td>@mdo</td>
	// </tr>
	// <tr>
	// <td>2</td>
	// <td>Jacob</td>
	// <td>Thornton</td>
	// <td>@fat</td>
	// </tr>
	// <tr>
	// <td>3</td>
	// <td>Larry</td>
	// <td>the Bird</td>
	// <td>@twitter</td>
	// </tr>
	public String getReport4Short(String username) {
		StringBuffer s = new StringBuffer();
		List<Reportdata> list = dao.getAll(getReportSqlfront + username
				+ getReportSqltail);

		for (Reportdata r : list) {
			s.append("<tr><td>").append(r.getReportNo()).append("</td><td>")
					.append(r.getInfo()).append("</td><td>")
					.append(r.getScope()).append("</td><td>")
					.append(r.getVal()).append("</td><td>")
					.append(r.getReportdate()).append("</td></tr>");
		}

		return s.toString();
	}

//	public String getRegResult(String userName, String passwd, String cards,
//			String SKUSERNAME, String SKPASSWD, String cardType,
//			HttpSession session) throws Exception {
//		StringBuffer sf = new StringBuffer();
//		boolean result = dao.register(userName, passwd, cards, SKUSERNAME,
//				SKPASSWD, cardType);
//		if (result) {
//			addSession(userName, passwd, session);
//			sf.append("注册成功!欢迎" + userName);
//		} else
//			sf.append(failregback);
//		return sf.toString();
//	}

	public String getRegResult(String userName, String passwd,
			HttpSession session) throws Exception {
		StringBuffer sf = new StringBuffer();
		boolean result = dao.register(userName, passwd);
		if (result) {
			addSession(userName, passwd, session);
			sf.append("注册成功!欢迎" + userName);
		} else
			throw new Exception(failregback);
		return sf.toString();
	}

	public List<RJReportInfo> getReport(String username, String startDate,
			String endDate, String kh) {

		// TODO Auto-generated method stub
		QueryHttpClient c = new QueryHttpClient();

		RJQueryReports rjq = new RJQueryReports();
		rjq.setEndDate(endDate);
		rjq.setStartDate(startDate);
		rjq.setKh(kh);
		rjq.setOperType("356");
		String str = ReportUtils.obj2gson(rjq);// 组装入参
		System.out.println("向仁济发送请求:"+str);
		String json = c.queryRj(str);// 获取结果

		RJReports rs = (RJReports) ReportUtils
				.str2Object(json, RJReports.class);
		List<RJReportInfo> rss = null;
		if (null != rs) {
			rss = rs.getReports();
			Collections.sort(rss);// 按时间逆序

		}

		return rss;
	}

	public RJReport getSingleReport(String username, String bgnum) {
		// TODO Auto-generated method stub

		// 20170523-SH01-1-G0561170-E
		RJQueryReport rjq = new RJQueryReport();
		rjq.setOperType("357");
		rjq.setBgdh(bgnum);
		String str = ReportUtils.obj2gson(rjq);
		System.out.println("查询json" + str);
		QueryHttpClient c = new QueryHttpClient();
		String json = c.queryRjReport(str);
		System.out.println("单个仁济报告结果" + json);
		StringBuffer s = new StringBuffer();
		RJReport rs = (RJReport) ReportUtils.str2Object(json, RJReport.class);
		

		return rs;

	}
}
